<?php
    session_start();
    $rssaddr = $_GET["rssaddr"];
    $rssdescription = $_GET["rssdescription"];

    function add_userrss($addr)
    {
        $sql_selectrssid = "select rss_id from rss where rss_addr='".$addr."'";
        $result_selectrssid = mysql_fetch_array(mysql_query($sql_selectrssid));
        $rssid = $result_selectrssid[0];
        $userid = $_SESSION["userid"];
        $sql_checkuserrss = "select count(user_rss_id) from user_rss where user_id='".$userid."' and rss_id='".$rssid."'";
        $result_checkuserrss = mysql_fetch_array(mysql_query($sql_checkuserrss));
        if($result_checkuserrss[0]==0)
        {
            $sql_insertuserrss = "insert into user_rss(user_id,rss_id) values('".$userid."','".$rssid."')";
            $result_insertuserrss = mysql_query($sql_insertuserrss);
            if($result_insertuserrss)
            {
                echo "添加rss成功！";
                mysql_close();
            }
            else
            {
                die("插入user_rss数据失败，请重试");
            }
        }
        else
        {
            echo ("您已经添加了该源");
        }
    }
    //echo $rssaddr;
    if(strlen($rssaddr)>0 && strlen($_SESSION["userid"])>0)
    {
        $con = mysql_connect('localhost','root','chenpo');
        if(!$con)
        {
            die('数据库连接有误:'.mysql_error());
        }
        mysql_select_db("reader",$con);
        $sql_checkrss = "select count(rss_id) from rss where rss_addr='".$rssaddr."'";
        $result_checkrss = mysql_fetch_array(mysql_query($sql_checkrss));
        if($result_checkrss[0]==0)
        {
            $sql_insertrss = "insert into rss(rss_addr,rss_description) values('".$rssaddr."','".$rssdescription."')";
            $result_insertrss = mysql_query($sql_insertrss);
            if($result_insertrss)
            {
                add_userrss($rssaddr);
            }
            else
            {
                die('插入表rss数据失败，请重试');
            }
        }
        else
        {
            add_userrss($rssaddr);
        }
    }
?>
